﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "UserBlock" in code, svc and config file together.
public class UserBlock : IUserBlock
{
    // get all nomal user
    public List<User> GetAllUser()
    {
        List<User> listUser = new List<User>();
        DataClassesDataContext db = new DataClassesDataContext();
        List<User_Table> users = db.User_Tables.Where(q => !q.Permission.Equals(true)).ToList();
        foreach (User_Table us in users)
        {
            User u = new User(us);
            listUser.Add(u);
        }
        return listUser;
    }
    // update status
    public void Block(Guid userID, bool status)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            List<User_Table> users = db.User_Tables.Where(q => q.UserID.Equals(userID)).ToList();
            User_Table user;
            if (users.Count == 1)
            {
                user = users[0];
                user.User_status = status;
                db.SubmitChanges();
            }
        }
        catch (SqlException ex)
        {

        }
        catch (Exception ex)
        {

        }
    }
    // get user by id
    public User GetUser(String user, String pass)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            User_Table ut = db.User_Tables.Where(q => (q.Username.Equals(user) && q.Password.Equals(pass))).First();
            return (new User(ut));
        }
        catch(Exception ex)
        {
            return null;
        }
    }
    //get user by User Name
    public List<User> GetUserByName(String userName)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var uts = db.User_Tables.Where(q => q.Username.Equals(userName));
            List<User> users = new List<User>();
            foreach (User_Table ut in uts)
            {
                users.Add(new User(ut));
            }
            return users;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
}
